package com.cisetech.web.bobocai.houtai;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cisetech.service.ColumnService;
import com.cisetech.service.ContentService;
import com.cisetech.vo.Page;
import com.cisetech.vo.bobocai.LContent;
import com.cisetech.vo.bobocai.LResources;
import com.cisetech.vo.bobocai.LUser;

//栏目管理控制器
@Controller
@RequestMapping(value="/ht/column")
public class ColumnController {
	@Autowired
	ColumnService columnService;
	@Autowired
	ContentService contentService;
	
	@RequestMapping(value="",method=RequestMethod.GET)
	public ModelAndView index(HttpServletResponse response,HttpServletRequest request){
		LUser user = LoginController.getUser();
		ModelAndView v = new ModelAndView("houtai/column");
		return v;
	}
	@ResponseBody
	@RequestMapping(value="/getList",method=RequestMethod.POST)
	public Page<LResources> getListAdv(LResources resources,HttpServletRequest request,HttpServletResponse response){
		Page<LResources> page = columnService.getList(resources);
		return page;
	}
	@RequestMapping(value="/addCoumn",method=RequestMethod.GET)
	public String addCoumn(Model v,HttpServletResponse response,HttpServletRequest request) {
		String id = request.getParameter("id");
		LResources resources = new LResources();
		resources.setGrade("0");//最高级别
		resources.setAround("1");//前台为1
		if(id!=null && !"".equals(id)){
			resources.setId(Integer.parseInt(id));
			List<LResources> list = columnService.getListResources(resources);
			v.addAttribute("resources", list==null? new LResources() : list.get(0));
		}else{
			v.addAttribute("resources", new LResources());
		}
		
		return "houtai/columnAddAndUpdate";
	}
	@RequestMapping(value="/addCoumn",method=RequestMethod.POST)
	public String addCoumns(LResources resources,HttpServletResponse response,HttpServletRequest request){
		//resources.set
		columnService.addLResources(resources);
		return "redirect:/ht/column";
	}
	@ResponseBody
	@RequestMapping(value="/del",method=RequestMethod.POST)
	public String del(LResources resources,HttpServletRequest request,HttpServletResponse response){
		int i = columnService.del(resources);
		return i+"";
	}
	@RequestMapping(value="/addCoumnContent",method=RequestMethod.GET)
	public String addCoumnContent(LResources resources,Model model,HttpServletResponse response,HttpServletRequest request){
		if(resources!=null && !"".equals(resources.getId()) && resources!=null &&resources.getId()!=null && resources.getId()!=0){
			List<LResources> list = columnService.getListResources(resources);
			if(list!=null && list.size()>0 && "0".equals(list.get(0).getIspage())){
				LContent content = new LContent();
				content.setReserve(resources.getId()+"");
				List<LContent> listContent = contentService.findContent(content);
				if(listContent!=null && listContent.size()>0){
					model.addAttribute("content", listContent.get(0));
				}else{
					model.addAttribute("content", new LContent());
				}
				model.addAttribute("resources", list==null? new LResources() : list.get(0));
				return "houtai/addCoumnContent";//单页添加内容
			}else if(list!=null && list.size()>0 && "1".equals(list.get(0).getIspage())){
			}
			return "houtai/addCoumnContent";
		}else{
			//获取栏目列表
			LContent content = new LContent();
			content.setReserve(request.getParameter("reserve"));
			String contentId = request.getParameter("contentId");
			if("0".equals(contentId)){
				content.setId(Integer.MAX_VALUE);
			}else{
				content.setId(Integer.parseInt(request.getParameter("contentId")));
			}
			List<LContent> listContent = contentService.findColumnContent(content);
			if(listContent!=null && listContent.size()>0){
				model.addAttribute("content", listContent.get(0));
			}else{
				model.addAttribute("content", new LContent());
			}
			model.addAttribute("reserve", request.getParameter("reserve"));
			return "houtai/addContent";//栏目添加内容
		}
		
	}
	@ResponseBody
	@RequestMapping(value="/addCoumnContent",method=RequestMethod.POST)
	public String addCoumnContentPost(LContent content,HttpServletResponse response,HttpServletRequest request){
		
		
		return contentService.addContent(content,"1");
	}
	@RequestMapping(value="/contentList",method=RequestMethod.GET)
	public String contentList(LContent content,Model model,HttpServletResponse response,HttpServletRequest request){
		model.addAttribute("content", content);
		return "houtai/contentlist";
	}
	@ResponseBody
	@RequestMapping(value="/contentList",method=RequestMethod.POST)
	public Page<LContent> contentListpost(LContent content,HttpServletResponse response,HttpServletRequest request){
		Page<LContent> page = columnService.getList(content);
		return page;
	}
	@ResponseBody
	@RequestMapping(value="/addCoumnContents",method=RequestMethod.POST)
	public String addCoumnContents(LContent content,HttpServletResponse response,HttpServletRequest request){
		return contentService.addContent(content,"0");
	}
	@ResponseBody
	@RequestMapping(value="/deletContents",method=RequestMethod.POST)
	public String deletContents(LContent content,HttpServletResponse response,HttpServletRequest request){
		return contentService.deletContents(content.getId()+"")+"";
	}
}
